<?php

namespace app\common\model\marketing;

use addons\xyb\XypHelper;
use think\Model;


class Association extends Model
{

    // 表名
    protected $name = 'association';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    public static $grant_status_list = [
        1 => '进行中',
        2 => '已结束',
        3 => '已下架'
    ];
    public static $grant_method_list = [
        1 => '全部',
        2 => '指定区域',
        3 => '指定用户',
    ];

    protected $append = [
        'involved_users',
        'activity_jobs',
        'grant_status_text',
        'grant_method_text',
        'start_date_text',
        'create_date'
    ];

    public function getActivityJobsAttr($value, $data){
      if(isset($data['id']) && $data['id']){
          return AssociationUsers::where('association_id', $data['id'])->count('id');
      } elseif (isset($data['value']) && $data['value']){
          return AssociationUsers::where('association_id', $data['value'])->count('id');
      }
      return 0;
    }
    public function getInvolvedUsersAttr($value, $data){
        if(isset($data['association_id']) && $data['association_id']){
            return AssociationUsers::where('association_id', $data['association_id'])->count('id');
        } else {
            return 0;
        }
        //return Association::where('id', $data['association_id'])->value('name');
    }

    public function getGrantStatusTextAttr($value, $data){
        if(isset($data['grant_status']) && $data['grant_status']){
            $value = $value ? $value : (isset($data['grant_status']) ? $data['grant_status'] : '');
            return isset(self::$grant_status_list[$value]) ? self::$grant_status_list[$value] : '';
        }else {
            return '';
        }
    }
    public function getGrantMethodTextAttr($value, $data){
        if(isset($data['grant_method']) && $data['grant_method']){
            $value = $value ? $value : (isset($data['grant_method']) ? $data['grant_method'] : '');
            return isset(self::$grant_method_list[$value]) ? self::$grant_method_list[$value] : '';
        }else {
            return '';
        }
    }
    public function getStartDateTextAttr($value, $data){
        if(isset($data['start_time']) && $data['start_time']){
            return date('Y-m-d', $data['start_time']) .' ~ '. date('Y-m-d', $data['end_time']);
        } else {
            return '';
        }
    }

    public function getCreateDateAttr($value, $data){
        if(isset($data['createtime']) && $data['createtime']) {
            return date('Y-m-d H:i', $data['createtime']);
        }else {
            return '';
        }
    }
    public static function getAdminLists()
    {
        $keyword = request()->param('keyword', '');
        $deadline = request()->param('deadline/a');
        $grant_status = request()->param('grant_status/a');
        $field = '';
        $query = self::field($field);
        if ($keyword) {
            $query->where(function ($query) use ($keyword) {
                $query->where('id', 'like', "%{$keyword}%")
                    ->whereOr('name', 'like', "%{$keyword}%")
                    ->whereOr('describe', 'like', "%{$keyword}%");
            });
        }
        if ($deadline_arr = XypHelper::stringToArray($deadline)) {
            $query->where('start_time','>',  XypHelper::getTimestamp($deadline_arr[0]))
                ->where('end_time','<',  XypHelper::getTimestamp($deadline_arr[1]));
        }
        if ($grant_status = XypHelper::stringToArray($grant_status)) {
            $query->whereIn('grant_status', $grant_status);
        }
        return $query->order('id desc')->paginate(10);
    }

    public static function getAssociationJobsDetails($association_id){
        $association = self::field('id,name,start_time,end_time,grant_method,grant_agent_id')
            ->where('id', $association_id)->find();
        $jobs_lists = AssociationJobs::field('id,name,start_time,end_time,activity_method,involved_days,execute_days')
            ->where('association_id', $association_id)->order('id asc')->select();
        $association->jobs = $jobs_lists;
        if($jobs_lists){
            $association->start_date_time = (end($jobs_lists)->end_time) + 86400;
        } else {
            $association->start_date_time = $association->start_time;
        }
        return $association;
    }
}